﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WorkOrder.Business;
using System.Data;
using System.Drawing;

namespace WorkOrder
{
    public partial class addOther : System.Web.UI.Page
    {
        Business_Table_Other b_t_other = new Business_Table_Other();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserID"] == null || Session["UserID"].ToString() == "")
                {
                    Session.Clear();
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "windowhref", "<script type='text/javascript'>alert('对不起，您还没有登录系统！');window.parent.location.href='../login.aspx';</script>");
                }
                DataTable table = b_t_other.GetOthers();
                if (table != null && table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        if (table.Rows[i]["other_key"] != null && table.Rows[i]["other_key"].ToString() == "orderTimeOut")
                            txtTimeOut.Text = table.Rows[i]["other_value"].ToString();
                        if (table.Rows[i]["other_key"] != null && table.Rows[i]["other_key"].ToString() == "debuggingTimeout")
                            txtDebuggingTimeOut.Text = table.Rows[i]["other_value"].ToString();
                        if (table.Rows[i]["other_key"] != null && table.Rows[i]["other_key"].ToString() == "testTimeout")
                            txtTestTimeOut.Text = table.Rows[i]["other_value"].ToString();
                        if (table.Rows[i]["other_key"] != null && table.Rows[i]["other_key"].ToString() == "returnVisitTimeout")
                            txtVisitReturnTimeOut.Text = table.Rows[i]["other_value"].ToString();
                        if (table.Rows[i]["other_key"] != null && table.Rows[i]["other_key"].ToString() == "deliveryTimeout")
                            txtDeliveryTimeOut.Text = table.Rows[i]["other_value"].ToString(); 
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!new Business_Table_Permission().HasInsert(Session["UserID"].ToString(), "Table_Other"))
            {
                lblMsg.ForeColor = Color.Red;
                lblMsg.Text = "提示信息：您没有保存其他设置信息的权限！";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "wiowhref", "<script type='text/javascript'>alert('对不起，您没有保存其他设置信息的权限！');window.close();</script>");
                return;
            }
            bool tmp = false;
            if (txtTimeOut.Text.Trim() != "")
            {
                tmp = b_t_other.Insert("orderTimeOut", txtTimeOut.Text.Trim());
                if (!tmp)
                {
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "提示信息：保存工单时限失败！";
                    return;
                }
            }
            if (txtDebuggingTimeOut.Text.Trim() != "")
            {
                tmp = b_t_other.Insert("debuggingTimeout", txtDebuggingTimeOut.Text.Trim());
                if (!tmp)
                {
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "提示信息：保存软调工作表提交时限失败！";
                    return;
                }
            }
            if (txtTestTimeOut.Text.Trim() != "")
            {
                tmp = b_t_other.Insert("testTimeout", txtTestTimeOut.Text.Trim());
                if (!tmp)
                {
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "提示信息：保存拨测表提交时限失败！";
                    return;
                }
            }
            if (txtVisitReturnTimeOut.Text.Trim() != "")
            {
                tmp = b_t_other.Insert("returnVisitTimeout", txtVisitReturnTimeOut.Text.Trim());
                if (!tmp)
                {
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "提示信息：保存回访时限失败！";
                    return;
                }
            }
            if (txtDeliveryTimeOut.Text.Trim() != "")
            {
                tmp = b_t_other.Insert("deliveryTimeout", txtDeliveryTimeOut.Text.Trim());
                if (!tmp)
                {
                    lblMsg.ForeColor = Color.Red;
                    lblMsg.Text = "提示信息：保存交付客户使用时限失败！";
                    return;
                }
            }
            if (tmp)
            {
                lblMsg.ForeColor = Color.Green;
                lblMsg.Text = "提示信息：保存成功！";
            }
        }
    }
}
